Document overview: Bio-image analysis documentation for smFISH quantitation
from matplotlib import pyplot as plt
import numpy as np
from skimage import (exposure, feature, filters, io, measure,
morphology, restoration, segmentation, transform,
util)
from skimage.morphology import remove_small_objects,dilation, erosion, ball
from skimage.measure import label, regionprops, regionprops_table
from skimage.morphology import disk
import pandas as pd
from skimage import img_as_uint
import napari
import czifile as cz
import skimage
from skimage.morphology import white_tophat, black_tophat, disk
import pyclesperanto_prototype as cle
from skimage.color import label2rgb
import cv2
from scipy import ndimage as ndi
from skimage.segmentation import watershed
from skimage.feature import peak_local_max
import glob
import os
import seaborn as sns
from statsmodels.formula.api import ols
from skimage.morphology import remove_small_objects, dilation, erosion, ball
from lxml import etree
from aicsimageio import AICSImage
from aicsimageio.writers import OmeTiffWriter
import stackview
import warnings
warnings.filterwarnings('ignore')
imgnames = sorted(glob.glob("D:\\PROJECTS\\MATT_GRAUS\\RNA\\IMAGES\\ALL\\*.czi"))
strip_text="D:\\PROJECTS\\MATT_GRAUS\\RNA\\IMAGES\\ALL\\"
out='D:\\PROJECTS\\MATT_GRAUS\RNA\\OUT\\'
os.chdir(out)
VESSEL_DF = []
PROBES_DF = []
gapdh_DF= []
Vessel_Raw_Images = []
Probes_Raw_Images = []
gadph_Raw_Images = []
Vessel_Mask_Images = []
Red_Blood_Masks = []
Probes_Mask = []
for imgname in imgnames:
image = cz.imread(imgname)
img = AICSImage(imgname)
X_PIXEL_SIZE =(img.physical_pixel_sizes.X)
Y_PIXEL_SIZE =(img.physical_pixel_sizes.Y)
trim_name=imgname.lstrip(strip_text)
#print(trim_name)
C0=image[0, 0, 0, 0,:,:,0]
C1=image[0, 0, 1, 0,:,:,0]
C2=image[0, 0, 2, 0,:,:,0]
Vessel_Raw_Images.append(C2)
Probes_Raw_Images.append(C1)
gadph_Raw_Images.append(C0)
#VESSEL WORKFLOW
radius = 20
disk_kernel = morphology.disk(radius)
vessels_sub_background = morphology.white_tophat(C2, footprint=disk_kernel)
vessels_gaussian = skimage.filters.gaussian(vessels_sub_background, sigma=10)
vessels_median = filters.median(vessels_gaussian)
vessel_threhold_values = skimage.filters.threshold_isodata (vessels_gaussian)
vessels_binary = vessels_gaussian > vessel_threhold_values
vessels_binary_small_obj_remove = remove_small_objects(vessels_binary, min_size=1600, connectivity=3)
vessel_labels = label(vessels_binary_small_obj_remove)
VESSEL_INFO = regionprops_table(vessel_labels, intensity_image=C2, properties = ['label','area', 'mean_intensity'])
VESSEL_INFO =pd.DataFrame(VESSEL_INFO)
VESSEL_INFO['IMAGE_ID'] = trim_name
VESSEL_INFO['Vessel Area (um2)'] = VESSEL_INFO['area']* X_PIXEL_SIZE*Y_PIXEL_SIZE
VESSEL_INFO['VESSEL'] = VESSEL_INFO['label']
VESSEL_DF.append(VESSEL_INFO)
Vessel_Mask_Images.append(vessel_labels)
#gapdh INTENSITY
gapdh_INFO = regionprops_table(vessel_labels, intensity_image=C0, properties = ['label','area', 'mean_intensity'])
gapdh_INFO =pd.DataFrame(gapdh_INFO)
gapdh_INFO['VESSEL'] = VESSEL_INFO['label']
gapdh_INFO['IMAGE_ID'] = trim_name
gapdh_DF.append(gapdh_INFO)
#Unique Vessel IDS
unique_vessels=np.unique(vessel_labels)
unique_vessels = list(unique_vessels)
unique_vessels.remove(0)
#PROBE WORKFLOW
#Extract Red Blood Cells
radius = 10
disk_kernel = morphology.disk(radius)
probes_sub_background = morphology.white_tophat(C1, footprint=disk_kernel)
ret,probes_binary = cv2.threshold(probes_sub_background, 0, 255, cv2.THRESH_OTSU)
ret,red_blood_cell_binary = cv2.threshold(probes_sub_background, 0, 255, cv2.THRESH_OTSU)
minArea = 75
blood_cells = remove_small_objects(red_blood_cell_binary>0, min_size=minArea, connectivity=1)
blood_cells_invert =skimage.util.invert(blood_cells)
Red_Blood_Masks.append(blood_cells)
Probes_Mask.append(probes_binary)
for row in unique_vessels:
vessell_loop =vessel_labels.copy()
vessell_loop[vessell_loop!=row] = 0
vessell_loop[vessell_loop!=0] = 1
probes_copy=probes_binary.copy()
masked_probe_gray = vessell_loop * probes_copy
masked_probe_gray = blood_cells_invert * masked_probe_gray
skimage.io.imsave(out+trim_name+"____"+str(row)+"____"+"__probe_data_used.tif", img_as_uint(masked_probe_gray), check_contrast=False )
#Watershed and Labels
distance = ndi.distance_transform_edt(masked_probe_gray)
coords = peak_local_max(distance, footprint=np.ones((3, 3)), labels=masked_probe_gray)
mask = np.zeros(distance.shape, dtype=bool)
mask[tuple(coords.T)] = True
markers, _ = ndi.label(mask)
labels = watershed(-distance, markers, mask=masked_probe_gray)
PROBES_INFO = regionprops_table(labels, intensity_image=C1, properties = ['label','area', 'mean_intensity'])
PROBES_INFO =pd.DataFrame(PROBES_INFO)
PROBES_INFO['VESSEL'] = row
PROBES_INFO['IMAGE_ID'] = trim_name
PROBES_INFO['Probes Area (um2)'] = PROBES_INFO['area']* X_PIXEL_SIZE*Y_PIXEL_SIZE
#Okay, let red blood cells for now will be filtere dout based on size, acknowlging teh watershed is not perfect
#PROBES_INFO = PROBES_INFO.drop([int(PROBES_INFO['Probes Area (um2)']) < 2].index)
################
PROBES_DF.append(PROBES_INFO)
skimage.io.imsave(out+trim_name+"__VESSEL_SEG.tif", img_as_uint(vessel_labels), check_contrast=False )
skimage.io.imsave(out+trim_name+"__PROBES_BINARY.tif", img_as_uint(probes_binary), check_contrast=False)
skimage.io.imsave(out+trim_name+"__BLOOD_CELLS.tif", img_as_uint(blood_cells), check_contrast=False)
skimage.io.imsave(out+trim_name+"__C2.tif", img_as_uint(C2), check_contrast=False)
skimage.io.imsave(out+trim_name+"C1.tif", img_as_uint(C1), check_contrast=False)
skimage.io.imsave(out+trim_name+"C0.tif", img_as_uint(C0), check_contrast=False)
Vessel_Mask_Images = np.array(Vessel_Mask_Images)
Red_Blood_Masks = np.array(Red_Blood_Masks)
Probes_Mask= np.array(Probes_Mask)
Vessel_Raw_Images= np.array(Vessel_Raw_Images)
Probes_Raw_Images=np.array(Probes_Raw_Images)
gadph_Raw_Images=np.array(gadph_Raw_Images)
np.shape(Vessel_Mask_Images)
(55, 512, 512)
#Visualise the raw images and segmented images
#import napari
#viewer = napari.view_image(Vessel_Raw_Images)
#labels_layer = viewer.add_image(Probes_Raw_Images)
#labels_layer = viewer.add_image(gadph_Raw_Images)
#labels_layer = viewer.add_labels(Vessel_Mask_Images)
#labels_layer = viewer.add_image(Red_Blood_Masks)
#labels_layer = viewer.add_image(Probes_Mask)
#napari.run()
VESSEL_DF_MASTER = pd.concat(VESSEL_DF, axis=0)
VESSEL_DF_MASTER.columns = ['Vessel_Label', 'Vessel_Pixel Area', "Vessel Mean Intensity", "Image_Title", "Vessel Area um2", "Vessel_ID"]
PROBES_DF_MASTER = pd.concat(PROBES_DF, axis=0)
PROBES_DF_MASTER.columns = ['Probes_Label', 'Probes_Pixel Area', "Probes Mean Intensity", "Vessel_ID", "Image_Title", "Probes Area um2"]
gapdh_DF_MASTER = pd.concat(gapdh_DF, axis=0)
gapdh_DF_MASTER.columns = ['gapdh_Label_Vessels_Dupe', 'Vessel_Area_Dupe', "gapdh Mean Intensity", "Vessel_ID", "Image_Title"]
Master_DF=PROBES_DF_MASTER.merge(VESSEL_DF_MASTER,on=['Image_Title',"Vessel_ID"], how='left').merge(gapdh_DF_MASTER, on=['Image_Title',"Vessel_ID"], how='left')
Master_DF=Master_DF.drop(['Vessel_Label', "Vessel_Area_Dupe", "gapdh_Label_Vessels_Dupe" ], axis=1)
Master_DF["TRT"] = Master_DF["Image_Title"].str.extract("(mut|wt|WT)")[0] .str.lower()
Master_DF.to_csv(out+"smFISH_DATA.csv")